﻿using Architecture.Domain.Dto;
using Architecture.Domain.RBAC;
using Architecture.Repository.Impl;
using Architecture.Repository.Interface;
using Architecture.Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Architecture.Service
{
    public class RoleService : IRoleService
    {
        protected readonly IRoleRepository roleRepository;
        private readonly IRelatedRepository relatedRepository;
        private readonly IUserRepository userRepository;
        /// <summary>
        /// 角色构造函数
        /// </summary>
        /// <param name="roleRepository"></param>
        public RoleService(IRoleRepository roleRepository, IRelatedRepository relatedRepository, IUserRepository userRepository)
        {
            this.roleRepository = roleRepository;
            this.relatedRepository = relatedRepository;
            this.userRepository = userRepository;
        }
        /// <summary>
        /// 角色添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<int> AddAsync(Role model)
        {
            model.AddName = "张三";
            model.AddTime = DateTime.Now;
            return await roleRepository.AddAsync(model);
        }
        /// <summary>
        /// 角色列表
        /// </summary>
        /// <returns></returns>
        public async Task<PageListDto<Role>> GetAllAsync(int pageIndex, int pageSize, string? roleName)
        {
            PageListDto<Role> page = new PageListDto<Role>();
            var roleList = await roleRepository.GetAllAsync();
            roleList = roleList.Where(x=>!x.IsDelete).ToList();
            if(roleName != null)
            {
                roleList = roleList.Where(x=>x.RoleName.Contains(roleName)).ToList();
            }
            page.ListCount = roleList.Count;
            page.PageCount = (int)Math.Ceiling((page.ListCount * 1.0) / pageSize);
            page.Lists = roleList.OrderByDescending(x=>x.RoleId).Skip((pageIndex-1)*pageSize).Take(pageSize).ToList();
            return page;
        }
        /// <summary>
        /// 角色树形
        /// </summary>
        /// <returns></returns>
        public async Task<object> RoleGetAll()
        {
            var roleList = await roleRepository.GetAllAsync();
            var list = (from x in roleList
                        select new
                        {
                            value=x.RoleId,
                            label=x.RoleName
                        });
            return list;
        }
        /// <summary>
        /// 角色逻辑删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteAsync(int id)
        {
            var list = await roleRepository.GetAsync(id);
            list.IsDelete = true;
            return await roleRepository.UpdateAsync(list);
        }
        /// <summary>
        /// 角色查看详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Role> GetByIdAsync(int id)
        {
            return await roleRepository.GetAsync(id);
        }
        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<int> UpdateAsync(Role model)
        {
            var list= await roleRepository.GetAsync(model.RoleId);
            list.RoleName = model.RoleName;
            list.UpdateName = "李四";
            list.UpdateTime = DateTime.Now;
            return await roleRepository.UpdateAsync(list);
        }
        /// <summary>
        /// 用户角色关联添加
        /// </summary>
        /// <param name="related">添加实体</param>
        /// <returns></returns>
        public async Task<int> GetRelatedAdd(int roleId, List<int> userId)
        {
            foreach (var item in userId)
            {
                var related = new Related();
                related.RoleId = roleId;
                related.UserId = item;
                await relatedRepository.AddAsync(related);
            }
            return 1;
        }
        /// <summary>
        /// 反填员工，部门
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<object> GetRelatedFan(int id)
        {
            var userList = await userRepository.GetAllAsync();
            var list = (from a in await relatedRepository.GetAllAsync()
                        join b in await roleRepository.GetAllAsync()
                        on a.RoleId equals b.RoleId
                        join u in userList
                        on a.UserId equals u.UserId
                        where a.RoleId == id
                        select new
                        {
                            u.SectoriD,
                            a.UserId
                        });
            return list.Distinct();
        }
        /// <summary>
        /// 角色员工名称反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<object> GetUserInfo(int id)
        {
            var list= (from a in await relatedRepository.GetAllAsync()
                       join u in await userRepository.GetAllAsync()
                       on a.UserId equals u.UserId
                       where a.RoleId == id
                       select new
                       {
                           a.RelatId,
                           u.UserId,
                           u.UserName
                       });
            return list;
        }
        //真删除
        public async Task<int> RelatedDelete(Related model)
        {
            var list = await relatedRepository.GetAsync(model.RelatId);
            return await relatedRepository.DeleteAsync(list);
        }
    }
}
